package com.lyj.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lyj.pojo.Department;
import com.lyj.pojo.Employee;
import com.lyj.pojo.LayuiPage;
import com.lyj.service.DepartmentService;
import com.lyj.service.EmployeeService;
import com.lyj.util.LayuiData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private EmployeeService employeeService;

    @RequestMapping("/list.html")
    public String toList(){
        return "departmentList";
    }
    @RequestMapping("/addDept.html")
    public String toAdd(){
        return "addDepartment";
    }

    @RequestMapping("/list")
    public ResponseEntity list(LayuiPage layuiPage, Department dept){
        PageHelper.startPage(layuiPage.getPage(),layuiPage.getLimit());
        List<Department> depts =  departmentService.findAllDept(dept);
        return ResponseEntity.ok(LayuiData.toLayuiTableModel(new PageInfo(depts)));
    }
    @RequestMapping("/update.html")
    public ModelAndView toUpdate(ModelAndView mv,Integer id){
        mv.addObject("dept",departmentService.findDeptById(id));
        mv.setViewName("departmentUpdate");
        return mv;
    }

    @RequestMapping("/update")
    public ResponseEntity update(@RequestBody Department department){
        List<Department> allDept = departmentService.findAllDept(new Department());
        for (Department department1 : allDept) {
            if(department1.getName().equals(department.getName()) && department1.getId() != department.getId()){
                return ResponseEntity.ok("该部门名称已经存在");
            }
        }
        departmentService.updateDeptById(department);
        return ResponseEntity.ok("1");
    }
    @RequestMapping("/addDept")
    public ResponseEntity addDept(@RequestBody Department department){
        List<Department> allDept = departmentService.findAllDept(new Department());
        for (Department department1 : allDept) {
            if (department1.getName().equals(department.getName())){
                return ResponseEntity.ok("1");
            }
        }
        departmentService.addDepartment(department);
        return ResponseEntity.ok("2");
    }

    @RequestMapping("/delete/{id}")
    public ResponseEntity delete(@PathVariable Integer id){
        Employee employee = new Employee();
        employee.setDeptId(id);
        List<Employee> allEmp = employeeService.findAllEmp(employee);
        if (!allEmp.isEmpty()){
            return ResponseEntity.ok("该部门下有多名员工，不能删除");
        }
        departmentService.deleteDeptById(id);
        return ResponseEntity.ok("1");
    }



}
